//
//  main.m
//  TestLoadCommand
//
//  Created by Hello Cai on 2021/1/5.
//

#import <UIKit/UIKit.h>
#import <mach-o/fat.h>
#import <mach-o/loader.h>

// argc : 参数个数
// argv : 存放参数的数组
// argv[0] : 表示当前可执行文件的路径
/* 调试方式：
   Edit Scheme->Run->Arguments->Arguments Passed On Launch添加-l、-d参数，
   在main函数开头打断点，然后运行项目到手机上，看断点显示的argc、argv内容
 */

int main(int argc, char * argv[]) {
    @autoreleasepool {
        
        if (argc == 1) {
            printf("-l 查看Mach-O信息\n");
            return 0;
        }
        
        if (strcmp(argv[1], "-l") != 0) {
            printf("-l 查看Mach-O信息\n");
            return 0;
        }
        
        //手机某app的路径
        NSString *appPath = @"/var/mobile/Containers/Bundle/Application/C439FFEB-BB46-479D-B8F3-4B002585AC08/neteasemusic.app/neteasemusic";
        //把路径上的文件加载进内存
        NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:appPath];
        int length = sizeof(uint32_t);
        
        //读取最前面的4个字节的内容（magic number，魔数，用来标识文件类型）
        NSData *data = [handle readDataOfLength:length];
        
        uint32_t magicNumber;
        //取data前4个字节的内容赋值给magicNumber
        [data getBytes:&magicNumber length:length];
        
        if (magicNumber == FAT_MAGIC || magicNumber == FAT_CIGAM) {
            printf("FAT文件\n");
        }else if (magicNumber == MH_MAGIC || magicNumber == MH_CIGAM) {
            printf("非64bit的架构文件\n");
        }else if (magicNumber == MH_MAGIC_64 || magicNumber == MH_CIGAM_64) {
            printf("64bit的架构文件\n");
        }else {
            printf("读取失败\n");
        }
        
        printf("magicNumber = 0x%x\n", magicNumber);
    }
    return 0;
}
